import pymysql

class DBTools(object):

    @classmethod
    def __create_conn(cls):
        conn = pymysql.connect(host="192.168.13.135",port=3306,user="root",password="926983",database="pymysql",charset="utf8")
        return conn
    @classmethod
    def query_one(self,sql):
        my_conn = None
        my_cursor = None
        res =None
        try:
            my_conn = DBTools.__create_conn()
            my_cursor = my_conn.cursor()
            my_cursor.execute(sql)
            res = my_cursor.fetchone()

        except Exception as e:
            print("查询失败",e)
        finally:
            my_cursor.close()
            my_conn.close()
            return res
        # 创建链接
        #创建游标
        #关闭游标
        #关闭连接
    @classmethod
    def db_uid(self,sql):
        my_conn = None
        my_cursor = None
        res = None
        try:
            my_conn=DBTools.__create_conn()
            my_cursor = my_conn.cursor()
            my_cursor.execute(sql)
            print("Affected rows:",my_conn.affected_rows())
            my_conn.commit()
        except Exception as e:
            print("UID失败",e)
            my_conn.rollback()
        finally:
            my_cursor.close()
            my_conn.close()
        # 创建链接
        # 创建游标
        # 关闭游标
        # 关闭连接

if __name__ == '__main__':
    sql = "select * from t_book"
    db = DBTools.query_one(sql)
    print(db)